package com.yztc.youbadbad;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;

import com.bumptech.glide.Glide;
import com.bumptech.glide.integration.okhttp.OkHttpUrlLoader;
import com.bumptech.glide.load.model.GlideUrl;
import com.tiffany.greendao.DaoMaster;
import com.tiffany.greendao.DaoSession;
import com.yztc.youbadbad.helper.OkHttpClientHelper;

import java.io.InputStream;

import cn.bmob.v3.Bmob;

/**
 * Created by Tiffany.V.Chou on 2016/11/14.
 */

public class App extends Application {
    public static App app;

    public DaoMaster.DevOpenHelper helper;
    public DaoMaster daoMaster;
    public DaoSession daoSession;

    public SQLiteDatabase db;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        initGlide();

        initDatabase();
        //第一：默认初始化
        Bmob.initialize(this, "413fd005c84f90b4304af58738de63ae");
    }

    public static App newInstance(){
        return app;
    }

    private void initDatabase() {
        helper = new DaoMaster.DevOpenHelper(this, "db_grid", null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);

        daoSession = daoMaster.newSession();
    }

    public DaoSession getDaoSession(){
        return daoSession;
    }

    public SQLiteDatabase getDb(){
        return db;
    }

    private void initGlide() {
        Glide.get(this).register(
                GlideUrl.class,
                InputStream.class,
                new OkHttpUrlLoader.Factory(OkHttpClientHelper.getOkHttpSingletonInstance())
        );
    }
}
